﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class CompareCv : System.Web.UI.Page
    {
        private List<Cv> _selectedCv = new List<Cv>();
        private List<CvCompare> _compareCv = new List<CvCompare>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetCvFromSelectedApplicant();
            }
        }

        private void GetCvFromSelectedApplicant()
        {
            string educationsString = "";
            string experienceString = "";
            string meritsString = "";
            if (Session["SelectedCv"] != null)
            {
                var selectedApplicants = Session["SelectedCv"] as List<int>;
                foreach (var applicantId in selectedApplicants)
                {
                    _selectedCv.Add(CvDB.GetCvInstanceWithApplicantId(applicantId));
                }
                foreach (var cv in _selectedCv)
                {
                    var education = cv.Educations;
                    foreach (var description in education)
                    {
                        educationsString += description.Desription + "\r\n";
                    }
                    var merits = cv.Merits;
                    foreach (var merit in merits)
                    {
                        meritsString += merit.Desription + "\r\n";
                    }
                    var experiences = cv.Experiences;
                    foreach (var experience in experiences)
                    {
                        experienceString += experience.Description + "\r\n";
                    }
                    var applicant = ApplicantDB.GetApplicantOnId(cv.ApplicantApplicantId);
                    _compareCv.Add(new CvCompare()
                    {
                        Name = applicant.FirstName + " " + applicant.LastName,
                        PersonNumber = applicant.PersonNumber,
                        Address = applicant.Address,
                        City = applicant.City,
                        Zip = applicant.Zip,
                        Phone = applicant.Phone,
                        Email = applicant.Email,

                        ApplicantThis = applicant,
                        CvId = cv.CvId,
                        Miscellaneous = cv.Miscellaneous,
                        Interests = cv.Interests,
                        PersonalQualities = cv.PersonalQualities,
                        IsDeleted = cv.IsDeleted,
                        ApplicantId = cv.ApplicantApplicantId,
                        Merits = meritsString,
                        Education = educationsString,
                        Experiences = experienceString
                    });
                }

                ListViewCompareCv.DataSource = _compareCv;
                ListViewCompareCv.DataBind();
                Session["CvList"] = _compareCv;
            }
        }

        protected void ListViewCompareCv_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                var CvList = Session["CvList"] as List<CvCompare>;
                var listViewItem = e.Item as ListViewItem;

                var cvId = (int)ListViewCompareCv.DataKeys[listViewItem.DataItemIndex].Value;
                if (CvList.Count> 0)
                {
                    foreach (var cv in CvList)
                    {
                        if (cv.CvId == cvId)
                        {
                            Session["PrintCv"] = cv;
                            Response.Redirect("~/ShowCvForPrint.aspx");
                           
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //LabelException.Text = ex.Message;
            }
        }
    }
}